<?php defined('SMIT_PATH') or die('Access Denied');
/**
 *	forum/app.php - SwixsBoard Forum Application
 *	This is Smit Matterhorn - Build 24109
 *	
 *
 * @author		The SmitBoard Team & Community
 * @license		GNU GPL v3
 * @link		http://smitboard.sf.net
 * @since		Matterhorn
 */
 
class Forum extends SmitApp
{
	function init()
	{
		$this->base->loadHandler('forum');
	}

	function main()
	{
		$cats = array();
		$forums = array();

		/**
		 * if we have something in the cache
		 * load the cached information, it saves memory
		 * Uses half the memory when cached
		 */
		 
		 /**
		  * Do we have cached categories?
		  * ---------------------------------
		  * NEVER USE != USE !== (Strict Comparison), since if the cache exists
		  * it may be blank or 0, NULL & 0 can pass as FALSE in PHP
		  */
		if($this->base->cache->get('cats') !== FALSE)
		{
			// Fetch the cached categories
			$cats = $this->base->cache->get('cats');
		}

		 /**
		  * Do we have cached forums?
		  * ---------------------------------
		  * AS I SAID NEVER USE != USE !== (Strict Comparison)
		  * in the former 0, BLANK/NULL variables can pass as false
		  * While in the later if will only load the cache if the variable is anything
		  * but a boolean false
		  */
		if($this->base->cache->get('forums') !== FALSE)
		{
			// Fetch the pre-parsed forums
			$forums = $this->base->cache->get('forums');
		}

		/**
		 * We should only loop if the $cat OR $forums
		 * variables are empty, this saves time, memory & resources
		 */
		if(count($cats) == 0 || count($forums) == 0)
		{
			foreach($this->forum->get_all_forums() as $list)
			{
				if($list['parent'] == 0)
				{
					$cats[$list['id']] = $list;
				}
				else
				{
					$list['topic_count'] = number_format($list['topic_count']);
					$list['post_count'] = number_format($list['post_count']);
					$list['hit_count'] = number_format($list['hit_count']);
					$list['last_post_topic'] = '';
					$list['last_post_id'] = ($list['last_post_topic'] == NULL ? '--' : $list['last_post_title']);
					$list['description'] = $this->base->lib->post_parse($list['description']);

					$forums[$list['parent']][] = $list;
				}
			}
		}
		
		/**
		 * Add to our cache if nothing is cached
		 * Always use Strict Comparison to check if the cache exists
		 */
		if($this->base->cache->get('cats') === FALSE)
		{
			// Add the categories to the cache
			$this->base->cache->add('cats', $cats);
		}
		if($this->base->cache->get('forums') === FALSE)
		{
			// Add the forums to the cache
			$this->base->cache->add('forums', $forums);
		}

		/**
		 * Old Code
		foreach($this->base->forum->get_all_forums() as $list)
		{
			if(array_key_exists($list['parent'], $cats))
			{
				$forums[] = $list;
			}
		}
		*/

		//$this->base->output->setTitle($this->base->lang('pagetitles.index'));
		foreach($cats as $id => $cat)
		{
			$data = array();
			$data['cat'] = $cat;
			$data['forums'] = $forums[$id];
			$this->base->output->process('forum.main', $data);
		}
		$this->base->loadHandler('user');

		//var_dump($this->base->user->create('admin23', 'test', 'admfin@localhost'));
		//var_dump($this->base->user->authenticate('admin23', 'tesyt'));
		$this->base->output->flush();
	}

	public function view($id = 0)
	{
		if($id == 0)
		{
			$this->base->output->redirect();
		}
		else
		{
			$list = array();
			foreach($this->forum->get_forums_by_parent($id) as $subforum)
			{
				$subforum['topic_count'] = number_format($subforum['topic_count']);
				$subforum['post_count'] = number_format($subforum['post_count']);
				$subforum['hit_count'] = number_format($subforum['hit_count']);
				$subforum['last_post_topic'] = '';
				$subforum['last_post_id'] = ($subforum['last_post_topic'] == NULL ? '--' : $subforum['last_post_title']);
				$subforum['description'] = $this->base->lib->post_parse($subforum['description']);
				$list[] = $subforum;
			}

			$data = array();
			$data['has_subforums'] = (count($list) > 0) ? TRUE : FALSE;
			$data['forums'] = $list;
			$this->base->output->process('forum.view', $data);
			$this->base->output->flush();
		}
	}
}

?>